// 导入axios
import axios from 'axios'
import RsaUtil from '../connon/EncryptionRsaUtil'
import router from "../router";
import { Message ,Notification } from 'element-ui';

// 进行一些全局配置
// 公共路由(网络请求地址)
// axios.defaults.baseURL = 'http://127.0.0.1:8080'
axios.defaults.baseURL = 'http://101.43.248.240:8080/universal'
// 请求响应超时时间
axios.defaults.timeout = 50000

//请求拦截器
axios.interceptors.request.use(config => {
    ////console.log(config)
    config.headers.sign = window.sessionStorage.getItem('sign')
    return config
})

// 封装自己的get/post方法
export default {
    get: function (path = '', data = '') {

        return new Promise(function (resolve, reject) {
            axios.get(path+ "/" + data, {})
                .then(function (response) {
                    // 按需求来，这里我需要的是response.data，所以返回response.data，一般直接返回response

                    if (response.data.code==5001||response.data.code==5002){
                        Notification.error({
                            title: '错误',
                            message: response.data.message
                        })
                        router.push('/login')
                    }
                    if (response.data.code != 20000){
                        Notification.error({
                            title: '错误',
                            message: response.data.message
                        })
                    }
                    resolve(response.data)
                })
                .catch(function (error) {
                    reject(error)
                })
        })
    },
    post: function (path = '', data = {}) {
        console.log(data)
        let parameter = {
            sign: window.sessionStorage.getItem('sign'),
            postData: RsaUtil.encryption(JSON.stringify(data))
        }
        return new Promise(function (resolve, reject) {
            axios.post(path, parameter)
                .then(function (response) {

                    if (response.data.code===5001||response.data.code===5002){
                        Notification.error({
                            title: '错误',
                            message: response.data.message
                        })
                        router.push('/login')
                    }
                    if (response.data.code !== 20000){
                        Notification.error({
                            title: '错误',
                            message: response.data.message
                        })
                    }
                    resolve(response.data)

                })
                .catch(function (error) {
                    reject(error)
                })
        })
    },
    delete: function (path = '', data = '') {
        return new Promise(function (resolve, reject) {
            axios.delete(path + "/" + data, {})
                .then(function (response) {
                    if (response.data.code==5001||response.data.code==5002){
                        Notification.error({
                            title: '错误',
                            message: response.data.message
                        })
                        router.push('/login')
                    }
                    if (response.data.code != 20000){
                        Notification.error({
                            title: '错误',
                            message: response.data.message
                        })
                    }
                    resolve(response.data)
                })
                .catch(function (error) {
                    reject(error)
                })
        })
    },
    put: function (path = '', data = '') {
        let parameter = {
            sign: window.sessionStorage.getItem('sign'),
            postData: RsaUtil.encryption(JSON.stringify(data))
        }
        const _this=this;
        return new Promise(function (resolve, reject) {
            axios.put(path, parameter)
                .then(function (response) {
                    if (response.data.code===5001||response.data.code===5002){
                        Notification.error({
                            title: '错误',
                            message: response.data.message
                        })
                        router.push('/login')
                    }
                    if (response.data.code !== 20000){
                        Notification.error({
                            title: '错误',
                            message: response.data.message
                        })
                    }

                    resolve(response.data)
                })
                .catch(function (error) {
                    reject(error)
                })
        })
    }
}
